// API路由配置文件
// 统一管理所有后端API路由

// 导入环境配置
import config from './config'

// 基础配置
export const API_CONFIG = {
  BASE_URL: config.BASE_URL,
  TIMEOUT: config.TIMEOUT
}

// 用户相关路由
export const USER_ROUTES = {
  LOGIN: '/api/UserController/getUserByIdByPass',
  REGISTER: '/api/UserController/saveUser',
  GET_USER_BY_ID: '/api/UserController/getUserById',
  UPDATE_USER: '/api/UserController/updateUser'
}

// 商家相关路由
export const BUSINESS_ROUTES = {
  LIST_BUSINESS: '/api/BusinessController/listBusiness',
  LIST_BUSINESS_BY_SORT: '/api/BusinessController/listBusinessBySort',
  GET_BUSINESS_BY_ID: '/api/BusinessController/getBusinessById'
}

// 食品相关路由
export const FOOD_ROUTES = {
  LIST_FOOD_BY_BUSINESS_ID: '/api/FoodController/listFoodByBusinessId'
}

// 购物车相关路由
export const CART_ROUTES = {
  LIST_CART: '/api/CartController/listCart',
  SAVE_CART: '/api/CartController/saveCart',
  UPDATE_CART: '/api/CartController/updateCart',
  REMOVE_CART: '/api/CartController/removeCart'
}

// 订单相关路由
export const ORDER_ROUTES = {
  SAVE_ORDER: '/api/OrdersController/saveOrder',
  LIST_ORDERS_BY_USER_ID: '/api/OrdersController/listOrdersByUserId'
}

// 地址相关路由
export const ADDRESS_ROUTES = {
  LIST_DELIVERY_ADDRESS_BY_USER_ID: '/api/DeliveryAddressController/listDeliveryAddressByUserId',
  SAVE_DELIVERY_ADDRESS: '/api/DeliveryAddressController/saveDeliveryAddress',
  UPDATE_DELIVERY_ADDRESS: '/api/DeliveryAddressController/updateDeliveryAddress',
  REMOVE_DELIVERY_ADDRESS: '/api/DeliveryAddressController/removeDeliveryAddress'
}

// 所有路由的统一导出
export const ALL_ROUTES = {
  USER: USER_ROUTES,
  BUSINESS: BUSINESS_ROUTES,
  FOOD: FOOD_ROUTES,
  CART: CART_ROUTES,
  ORDER: ORDER_ROUTES,
  ADDRESS: ADDRESS_ROUTES
}

// 路由验证函数
export function validateRoute(route) {
  if (!route || typeof route !== 'string') {
    throw new Error('路由必须是有效的字符串')
  }
  if (!route.startsWith('/')) {
    throw new Error('路由必须以 "/" 开头')
  }
  return true
}

// 构建完整URL的工具函数
export function buildFullUrl(route, params = {}) {
  validateRoute(route)
  
  let url = `${API_CONFIG.BASE_URL}${route}`
  
  // 如果有查询参数，添加到URL中
  if (params && Object.keys(params).length > 0) {
    const searchParams = new URLSearchParams(params)
    url += `?${searchParams.toString()}`
  }
  
  return url
}

// 获取所有可用路由的函数（用于调试）
export function getAllAvailableRoutes() {
  const routes = []
  
  Object.keys(ALL_ROUTES).forEach(category => {
    Object.keys(ALL_ROUTES[category]).forEach(routeName => {
      routes.push({
        category,
        name: routeName,
        path: ALL_ROUTES[category][routeName],
        fullUrl: buildFullUrl(ALL_ROUTES[category][routeName])
      })
    })
  })
  
  return routes
}

export default ALL_ROUTES